import os
import random
import shutil
 
def movefile_rand(img, outfile, num):
    list_ = os.listdir(img)
    if num > len(list_):
        print('输出数量必须小于：', len(list_))
        exit()
    numlist = random.sample(range(0,len(list_)),num) # 生成随机数列表a
    cnt = 0
    for n in numlist:
        filename = list_[n]
        oldpath = os.path.join(img, filename)
        newpath = os.path.join(outfile, filename)
        shutil.move(oldpath, newpath)
        print('剩余文件：', num-cnt)
        cnt = cnt + 1
    print('==========task OK!==========')
    
if __name__ == "__main__":
#    path = '../validation/wood_floor'
#    if not os.path.exists(path):
#        os.makedirs(path)
#    movefile_rand('dining_table', path, 200) # 操作目录，输出目录，输出数量
#    movefile_rand('wood_floor', path, int(len(os.listdir('wood_floor')) * 0.2))
    
    path='/home/ailab2/Documents/train_data/detection/100w_200w_no_cali_20200914/augFiles'
    path2='/home/ailab2/Documents/train_data/detection/100w_200w_no_cali_20200914/detection_platform_cali_20200914_flip'
    
    for fol in os.listdir(path):
        os.mkdir(path2+'/'+fol)
        mount = len(os.listdir(path+'/'+fol))
        # movefile_rand((path+'/'+fol), (path2+'/'+fol), int(mount * 0.5))
        movefile_rand((path+'/'+fol), (path2+'/'+fol), mount)
        
            